//
//  XRCommonPopTipView.m
//  LiveIMTool
//
//  Created by NeiQuan on 16/10/27.
//  Copyright © 2016年 AlexiChen. All rights reserved.
//

#import "XRCommonPopTipView.h"
#import <objc/runtime.h>
static UIButton *_cover;
static  const  void *XRCommonPopTipViewkey= &XRCommonPopTipViewkey;
@implementation XRCommonPopTipView
+(void)popMenuWithtitletext:(NSString *)text contentText:(NSString *)content toPopView:(UIView *)view type:(BtnBlock)block
{
    [XRCommonPopTipView popMenuWithtitletext:text contentText:content  popViewType:SendKeyDoubleButton toPopView:view type:^(SendKeyBtnType type)
     {
        block(type);
    }];
}
+(void)popMenuWithtitletext:(NSString *)text contentText:(NSString *)content popViewType:(XRCommonPopTipViewType)tipType  toPopView:(UIView *)view type:(BtnBlock)block
{
    [XRCommonPopTipView popMenuWithtitletext:text contentText:content contextAlin:NSTextAlignmentCenter popViewType:tipType toPopView:view type:^(SendKeyBtnType type) {
         block(type);
    }];
    
}
+(void)popMenuWithtitletext:(NSString *)text contentText:(NSString *)content contextAlin:(NSTextAlignment)contextAlin popViewType:(XRCommonPopTipViewType)tipType  toPopView:(UIView *)view type:(BtnBlock)block
{
    [XRCommonPopTipView popMenuWithtitletext:text contentText:content canceltitle:@"取消" confirmTitle:@"确定" contextAlin:contextAlin popViewType:tipType toPopView:view type:^(SendKeyBtnType type) {
         block(type);
    }];
}
+(void)popMenuWithtitletext:(NSString *)text contentText:(NSString *)content  canceltitle:(NSString *)cancletitle confirmTitle:(NSString *)confirmTitle toPopView:(UIView *)view type:(BtnBlock)block
{
    [XRCommonPopTipView popMenuWithtitletext:text contentText:content canceltitle:cancletitle confirmTitle:confirmTitle contextAlin:NSTextAlignmentCenter popViewType:SendKeyDoubleButton toPopView:view type:^(SendKeyBtnType type)
     {
        block(type);
    }];
}
+(void)popMenuWithtitletext:(NSString *)text contentText:(NSString *)content  canceltitle:(NSString *)cancletitle confirmTitle:(NSString *)confirmTitle  contextAlin:(NSTextAlignment)contextAlin popViewType:(XRCommonPopTipViewType)tipType toPopView:(UIView *)view type:(BtnBlock)block

{
    _cover = [[UIButton alloc] init];
    [_cover addTarget:self action:@selector(clickCover:) forControlEvents:UIControlEventTouchUpInside];
    _cover.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.6];
    _cover.frame = [UIScreen mainScreen].bounds;
    _cover.tag=1000000;
    [[UIApplication sharedApplication].keyWindow addSubview:_cover];
    UIView *sendView=[[UIView alloc] initWithFrame:CGRectMake(30, 0, kMainScreenWidth-60, (kMainScreenWidth-60)*9/16)];
    sendView.layer.masksToBounds=YES;
    sendView.layer.cornerRadius=5.0f;
    sendView.center=_cover.center;
    [sendView  setBackgroundColor: [UIColor whiteColor]];
    sendView.transform=CGAffineTransformScale( sendView.transform, 1.05, 1.05);
    [_cover addSubview:sendView];
    [UIView animateWithDuration:0.2 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        sendView.transform=CGAffineTransformScale( sendView.transform, 0.96, 0.96);
    } completion:^(BOOL finished)
     {
         
     }];
    UILabel  *titleLable=[[UILabel alloc] init];
    [sendView addSubview:titleLable];
    titleLable.textColor=[UIColor blackColor];
    titleLable.text=text;
    titleLable.textAlignment=NSTextAlignmentCenter;
    titleLable.font=[UIFont systemFontOfSize:16];
    titleLable.textColor=[UIColor blackColor];
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(21);
        make.left.equalTo(sendView).with.offset(12);
        make.top.equalTo(sendView).with.offset(18);
        make.right.equalTo(sendView).with.offset(12);
    }];
    UILabel  *contentLable=[[UILabel alloc] init];
    [sendView addSubview:contentLable];
    contentLable.textColor=[UIColor blackColor];
    contentLable.text=content;
    contentLable.numberOfLines=0;
    contentLable.textAlignment=contextAlin;
    contentLable.font=[UIFont systemFontOfSize:14];
    contentLable.textColor=[UIColor grayColor];
    
    [contentLable mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.bottom.equalTo(sendView).with.offset(45);
         make.left.equalTo(sendView).with.offset(12);
         make.right.equalTo(sendView).with.offset(-12);
         make.top.equalTo(titleLable.mas_bottom).with.offset(3);
     }];
    if (tipType==SendKeyDoubleButton)
    {
        UIButton *cancelButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [sendView addSubview:cancelButton];
        [cancelButton setTitle:cancletitle forState:UIControlStateNormal];
        cancelButton.titleLabel.font=[UIFont systemFontOfSize:16];
        cancelButton.tag=50001;
        [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(kMainScreenWidth*0.5-30);
            make.bottom.left.equalTo(sendView);
        }];
        [cancelButton setTitleColor:[UIColor colorWithNSString:@"#888888"] forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [sendView addSubview:doneButton];
        [doneButton setTitle:confirmTitle forState:UIControlStateNormal];
        doneButton.tag=50000;
        doneButton.titleLabel.font=[UIFont systemFontOfSize:16];
        [doneButton mas_makeConstraints:^(MASConstraintMaker *make)
         {   make.height.mas_equalTo(45);
             make.width.mas_equalTo(kMainScreenWidth*0.5-30);
             make.bottom.right.equalTo(sendView);
         }];
        [doneButton setTitleColor: kNavBarThemeColor forState:UIControlStateNormal];
        [doneButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        objc_setAssociatedObject(self, XRCommonPopTipViewkey, block, OBJC_ASSOCIATION_COPY);
        UIView  *lineBottomView=[[UIView alloc] init];
        [lineBottomView setBackgroundColor:[UIColor colorWithHexString:@"0xdddddd"]];
        [sendView addSubview:lineBottomView];
        [lineBottomView  mas_makeConstraints:^(MASConstraintMaker *make)
         {   make.bottom.equalTo(doneButton.mas_top);
             make.height.mas_equalTo(1.0);
             make.left.right.equalTo(sendView);
         }];
        UIView  *lineCenterView=[[UIView alloc] init];
        [lineCenterView setBackgroundColor:[UIColor colorWithHexString:@"0xdddddd"]];
        [sendView addSubview:lineCenterView];
        [lineCenterView  mas_makeConstraints:^(MASConstraintMaker *make){
            make.width.mas_equalTo(1.0);
            make.height.equalTo(doneButton.mas_height);
            make.bottom.equalTo(sendView);
            make.left.equalTo(cancelButton.mas_right);
        }];
    }else
    {
        UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [sendView addSubview:doneButton];
        [doneButton setTitle:@"确定" forState:UIControlStateNormal];
        doneButton.tag=50000;
        doneButton.titleLabel.font=[UIFont systemFontOfSize:16];
        
        [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.equalTo(sendView);
        }];
        [doneButton setTitleColor:kNavBarThemeColor forState:UIControlStateNormal];
        [doneButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        objc_setAssociatedObject(self, XRCommonPopTipViewkey, block, OBJC_ASSOCIATION_COPY);
        UIView  *lineBottomView=[[UIView alloc] init];
        [lineBottomView setBackgroundColor:[UIColor colorWithHexString:@"0xdddddd"]];
        [sendView addSubview:lineBottomView];
        [lineBottomView  mas_makeConstraints:^(MASConstraintMaker *make)
         {   make.bottom.equalTo(doneButton.mas_top);
             make.height.mas_equalTo(1.0);
             make.left.right.equalTo(sendView);
         }];
    }

}
+(void)buttonClick:(UIButton *)sender
{
    [_cover removeFromSuperview];
    BtnBlock block=objc_getAssociatedObject(self, XRCommonPopTipViewkey);
    if (block)
    {
        block(sender.tag-50000);
    }
}
#pragma mark --移除提示框
+ (void)clickCover:(UIButton *)btn
{
    [btn removeFromSuperview];
    BtnBlock block=objc_getAssociatedObject(self, XRCommonPopTipViewkey);
    if (block)
    {
        block(1);
    }
}
@end
